package chinaums.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import chinaums.dao.UrlMapper;
import chinaums.entity.GetLongUrl;
import chinaums.entity.LongUrlLink;
import chinaums.entity.Url;
import chinaums.service.IUrlService;
import chinaums.service.LongUrlLinkService;

/**
 * <p>Title:UrlServiceImpl</p>
 * <p>Description:短连接的实现层</p>
 * @version v1.0
 * @author RenLei
 * 2017年7月21日
 */
@Service
public class UrlServiceImpl implements IUrlService {
	private static Logger log= LoggerFactory.getLogger(UrlServiceImpl.class);
	@Autowired
    private UrlMapper urlMapper;
	@Autowired
	private GetLongUrlServiceImpl getMapper;
	@Autowired
	private LongUrlLinkService linkService;
	private long totaltime=0;
	@Override
	public String insertUrl(String getlistid) {
		try {
			log.info("到新增长链接了.........");
			long startTime=System.currentTimeMillis();
			//获取ids集合根据getlistid
			List<GetLongUrl> list=getMapper.selectInsertInfomation(getlistid);
			
			if(list==null || list.size() <= 0){
				return "fail01";
			}
			for(int i=0;i<list.size();i++){
				//得到长连接集合
				String url1= list.get(i).getIds();
				//长链接以逗号分割，储存到数组里面
				String[] ulist=url1.split(",");
				//添加到集合中
				ArrayList<String> arraylist=new ArrayList<String>();
				Collections.addAll(arraylist, ulist);
				log.info("获取到的长连接的总长度为："+arraylist.size());
				log.info("开始新增长链接了，准备入库.........");
				//定义一个集合，将值添加到linkList
				List<LongUrlLink> linkList=new ArrayList<LongUrlLink>();
				for (int j = 0; j < arraylist.size(); j++) {
					//新增，将传过来的数据插入到表中
					LongUrlLink link=new LongUrlLink();
					link.setChannelid(Integer.parseInt(getlistid));
					link.setLongurl(arraylist.get(j));
					linkList.add(link);
				}
				//分割数据，将值插入数据库
				List<LongUrlLink> subList=new ArrayList<LongUrlLink>();
				int longListSize=linkList.size();
				log.info("linkList集合中的数量是:"+longListSize);
				//定义count数量
				int size=1000;
				//定义循环次数
				int cycleCount=0;
				//判断集合是否小于count,如果小于就只循环一次，count等于集合大小
				if(longListSize<=size){//加一个！=0的判断
					size=longListSize;
					cycleCount=longListSize/size;
				}else{
					if(longListSize%size==0){
						cycleCount=longListSize/size;
					}else{
						cycleCount=(longListSize/size)+1;
					}
				}
				for(int l=0;l<cycleCount;l++){
					if((l+1)==cycleCount){
		                int startIndex = (l*size);
		                int endIndex = linkList.size();
		                subList =linkList.subList(startIndex,endIndex);
		            }else{
		                int startIndex = l*size;
		                int endIndex = (l+1)*size;
		                subList =linkList.subList(startIndex,endIndex);
		            }
					log.info("subList每次的数量是:"+subList.size());
				    linkService.insertLongUrl(subList);
				}
				log.info("插入成功.........");
			}
		long endTime=System.currentTimeMillis();
		totaltime=endTime-startTime;
		} catch (Exception e) {
			System.out.println(e);
			return "fail02";
		}
		return "success";
	}
	@Override
	public List<Url> selectUrl(Integer id) {
		return urlMapper.selectUrl(id);
	}
	@Override
	public int updateUrl(Url url) {
		// TODO Auto-generated method stub
		return urlMapper.updateUrl(url);
	}
	@Override
	public int addUrl(List<Url> list) {
		// TODO Auto-generated method stub
		return urlMapper.insertUrl(list);
	}
	@Override
	public List<Url> selectErrorUrl() {
		// TODO Auto-generated method stub
		return urlMapper.selectErrorUrl();
	}
	@Override
	public int updateErrorUrl(Url url) {
		// TODO Auto-generated method stub
		return urlMapper.updateErrorUrl(url);
	}
	@Override
	public int updateShortVersion(Url url) {
		// TODO Auto-generated method stub
		return urlMapper.updateShortVersion(url);
	}
	
}
